package com.lbt.weixin.server.controller;

import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lbt.framework.exception.MyException;
import com.lbt.framework.general.Searching;
import com.lbt.framework.jackson.result.PageResult;
import com.lbt.framework.jackson.result.StatusResult;
import com.lbt.weixin.common.bean.entity.Car;
import com.lbt.weixin.common.bean.entity.User;
import com.lbt.weixin.common.bean.search.Car4Search;
import com.lbt.weixin.common.service.CarService;
import com.lbt.weixin.server.common.DateUtil;
import com.lbt.weixin.server.common.LbtUtil;
import com.lbt.weixin.server.jackson.filter.CarFilter;


@Controller
@RequestMapping("/car")
public class CarController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(CarController.class);
	
	@Resource
	CarService carService;
	
	/**
	 * 查询
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public PageResult<?> get(Car4Search car4s, Searching si, HttpServletRequest request) throws MyException {
		long userId = checkBind(request);
		log.debug("获取用户车牌列表: userId={}", userId);
		PageResult<Car> result = new PageResult<Car>();
		result.setFilter(CarFilter.get());
		User user = new User();
		user.setUserId(userId);
		car4s.setUser(user);
		result.setResult(carService.list(car4s, si));
		result.setOk();
		return result;
	}
	
	/**
	 * 添加
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public StatusResult add(String carNo, String beginDate, HttpServletRequest request) throws MyException {
		long userId = checkBind(request);
		log.debug("添加车牌号: userId={}, carNo={}, beginDate={}", 
				new Object[]{userId, carNo, beginDate});
		User user = userService.one(userId);
		
		String p = "{\"CarCardsNo\":\""+ carNo +"\",\"Telephone\":\""+ user.getMobile() +"\",\"BeginDate\":\""+ beginDate +"\"}";
		LbtUtil.getApiResult(p, "checkuser");
		
		StatusResult result = new StatusResult();
		
		// 添加车牌信息
		Car car = new Car();
		car.setCarNo(carNo);
		car.setUser(user);
		int count = carService.count(car);
		if (count == 0) {
			car.setIsDefault(false);
			car.setAddTime(DateUtil.getTimeStamp());
			carService.persist(car);
			setDefault(car.getCarId(), request);
		} else {
			result.setFailMessage("车辆已经存在，请不要重复添加。");
			return result;
		}
		
		result.setOk();
		return result;
	}

	@RequestMapping("/login")
	@ResponseBody
	public StatusResult login(String carNo, HttpServletRequest request) throws MyException {
		long userId = checkBind(request);
		log.debug("切换车牌号: userId={}，carNo={}", userId, carNo);
		request.getSession().setAttribute("carNo", carNo);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	@RequestMapping("/setDefault")
	@ResponseBody
	public StatusResult setDefault(long carId, HttpServletRequest request) throws MyException {
		long userId = checkBind(request);
		log.debug("设置默认车辆: userId={}，carId={}", userId, carId);
		User user = userService.one(userId);
		Set<Car> cars = user.getCars();
		for (Car car : cars) {
			car.setIsDefault(carId == car.getCarId());
			carService.merge(car);
		}
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
}
